package com.xoa.service.cms.impl;


import com.xoa.dao.cms.SiteMapper;
import com.xoa.model.cms.Site;
import com.xoa.service.cms.SiteService;

import com.xoa.util.ToJson;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;


@Service
public class SiteServiceImpl implements SiteService {

    @Resource
    private SiteMapper siteMapper;

    @Override
    @Transactional
    public ToJson<Site> selectSite() {

        ToJson<Site> json = json = new ToJson<Site>();
        List<Site> list =  siteMapper.selectSite();
        try {

            if (list.size() > 0) {
                json.setObj(list);
                json.setMsg("ok");
                json.setFlag(0);
            }
        } catch (Exception e) {
            json.setFlag(1);
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return json;
    }
    @Override
    @Transactional
    public ToJson<Site> selectSiteById(Site site) {

        ToJson<Site> json = json = new ToJson<Site>();

        try {
            Site site1 =  siteMapper.selectByPrimaryKey(site.getPortalId());
                json.setObject(site1);
                json.setMsg("ok");
                json.setFlag(0);

        } catch (Exception e) {
            json.setFlag(1);
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return json;
    }
    @Override
    @Transactional
    public ToJson<Site> insertSite(HttpServletRequest request,Site site) {
        ToJson<Site> toJson = new ToJson<Site>(1, "error");

        try {
            int temp = siteMapper.insertSelective(site);
            if (temp > 0) {
                toJson.setFlag(0);
                toJson.setMsg("ok");
            }
        } catch (Exception e) {
            toJson.setFlag(1);
            toJson.setMsg(e.getMessage());
            e.printStackTrace();
        }

        //在模板文件夹下创建对应的文件夹
        //获取WEB-ROOT的路径,添加cmsTmp文件夹

        String rootPath = request.getSession().getServletContext().getRealPath("/");
        File path = new File(rootPath,"cmsTmp");
        if(!path.exists()){
            path.mkdirs();
        }

        //创建对应id的文件夹
        if(site.getPortalId()!=null && site.getPortalId()!=0) {
            File cmsFile = new File(path, site.getPortalId() + "");
            if(!cmsFile.exists()){
                cmsFile.mkdirs();
            }
        }
        return toJson;
    }

    @Override
    @Transactional
    public ToJson<Site> upSite(Site site) {
        ToJson<Site> toJson = new ToJson<Site>(1, "error");

        try {
            int temp = siteMapper.updateByPrimaryKeySelective(site);
            if (temp > 0) {
                toJson.setFlag(0);
                toJson.setMsg("ok");
            }
        } catch (Exception e) {
            toJson.setFlag(1);
            toJson.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return toJson;
    }

    @Override
    @Transactional
    public ToJson<Site> delSite(Site site) {
        ToJson<Site> toJson = new ToJson<Site>(1, "error");

        try {
            int temp = siteMapper.delSite(site);
            if (temp > 0) {
                toJson.setFlag(0);
                toJson.setMsg("ok");
            }
        } catch (Exception e) {
            toJson.setFlag(1);
            toJson.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return toJson;
    }
    @Override
    @Transactional
    public ToJson<Site> selectSiteByTemp(Site site) {

        ToJson<Site> json = json = new ToJson<Site>();

        try {
            Site site1 =  siteMapper.selectSiteByTemp(site.getPortalId());
            json.setObject(site1);
            json.setMsg("ok");
            json.setFlag(0);

        } catch (Exception e) {
            json.setFlag(1);
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return json;
    }

}